﻿using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;
using Usas.Model.Cms;
namespace Usas.BLL.Cms
{
    /// <summary>
    /// 业务逻辑类cmsArticle 的摘要说明。
    /// </summary>
    public class cmsArticle
    {
        private readonly Usas.DAL.Cms.cmsArticle dal = new Usas.DAL.Cms.cmsArticle();
        public cmsArticle()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ArtId)
        {
            return dal.Exists(ArtId);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cmsArticle model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cmsArticle model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(long ArtId)
        {

            dal.Delete(ArtId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cmsArticle GetModel(long ArtId)
        {

            return dal.GetModel(ArtId);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public Usas.Model.Cms.cmsArticle GetModelByCache(long ArtId)
        {

            string CacheKey = "cmsArticleModel-" + ArtId;
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ArtId);
                    if (objModel != null)
                    {
                        int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Usas.Model.Cms.cmsArticle)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Usas.Model.Cms.cmsArticle> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Usas.Model.Cms.cmsArticle> DataTableToList(DataTable dt)
        {
            List<Usas.Model.Cms.cmsArticle> modelList = new List<Usas.Model.Cms.cmsArticle>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Usas.Model.Cms.cmsArticle model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Usas.Model.Cms.cmsArticle();
                    //model.ArtId=dt.Rows[n]["ArtId"].ToString();
                    if (dt.Rows[n]["TypeId"].ToString() != "")
                    {
                        model.TypeId = int.Parse(dt.Rows[n]["TypeId"].ToString());
                    }
                    if (dt.Rows[n]["BorderId"].ToString() != "")
                    {
                        model.BorderId = int.Parse(dt.Rows[n]["BorderId"].ToString());
                    }
                    //model.ParentArtId=dt.Rows[n]["ParentArtId"].ToString();
                    //model.ArtOrder=dt.Rows[n]["ArtOrder"].ToString();
                    model.ArtDirectory = dt.Rows[n]["ArtDirectory"].ToString();
                    model.ArtTopic = dt.Rows[n]["ArtTopic"].ToString();
                    model.ArtKeys = dt.Rows[n]["ArtKeys"].ToString();
                    model.ArtFrom = dt.Rows[n]["ArtFrom"].ToString();
                    model.ArtSummary = dt.Rows[n]["ArtSummary"].ToString();
                    model.ArtFileName = dt.Rows[n]["ArtFileName"].ToString();
                    if (dt.Rows[n]["ModfiyTime"].ToString() != "")
                    {
                        model.ModfiyTime = DateTime.Parse(dt.Rows[n]["ModfiyTime"].ToString());
                    }
                    if (dt.Rows[n]["hits"].ToString() != "")
                    {
                        model.hits = int.Parse(dt.Rows[n]["hits"].ToString());
                    }
                    if (dt.Rows[n]["Modify"].ToString() != "")
                    {
                        model.Modify = int.Parse(dt.Rows[n]["Modify"].ToString());
                    }
                    if (dt.Rows[n]["Writer"].ToString() != "")
                    {
                        model.Writer = int.Parse(dt.Rows[n]["Writer"].ToString());
                    }
                    model.Auditer = dt.Rows[n]["Auditer"].ToString();
                    if (dt.Rows[n]["Audit"].ToString() != "")
                    {
                        if ((dt.Rows[n]["Audit"].ToString() == "1") || (dt.Rows[n]["Audit"].ToString().ToLower() == "true"))
                        {
                            model.Audit = true;
                        }
                        else
                        {
                            model.Audit = false;
                        }
                    }
                    if (dt.Rows[n]["ArtFormat"].ToString() != "")
                    {
                        model.ArtFormat = int.Parse(dt.Rows[n]["ArtFormat"].ToString());
                    }
                    if (dt.Rows[n]["Version"].ToString() != "")
                    {
                        model.Version = decimal.Parse(dt.Rows[n]["Version"].ToString());
                    }
                    if (dt.Rows[n]["CanView"].ToString() != "")
                    {
                        if ((dt.Rows[n]["CanView"].ToString() == "1") || (dt.Rows[n]["CanView"].ToString().ToLower() == "true"))
                        {
                            model.CanView = true;
                        }
                        else
                        {
                            model.CanView = false;
                        }
                    }
                    model.ArtViewPurview = dt.Rows[n]["ArtViewPurview"].ToString();
                    model.ArtEditPurview = dt.Rows[n]["ArtEditPurview"].ToString();
                 
                    if (dt.Rows[n]["ShowOrder"].ToString() != "")
                    {
                        if ((dt.Rows[n]["ShowOrder"].ToString() == "1") || (dt.Rows[n]["ShowOrder"].ToString().ToLower() == "true"))
                        {
                            model.ShowOrder = true;
                        }
                        else
                        {
                            model.ShowOrder = false;
                        }
                    }
                    if (dt.Rows[n]["IsVersion"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsVersion"].ToString() == "1") || (dt.Rows[n]["IsVersion"].ToString().ToLower() == "true"))
                        {
                            model.IsVersion = true;
                        }
                        else
                        {
                            model.IsVersion = false;
                        }
                    }
                    model.IsTop = dt.Rows[n]["IsTop"].ToString();
                    if (dt.Rows[n]["PublishStatus"].ToString() != "")
                    {
                        model.PublishStatus = int.Parse(dt.Rows[n]["PublishStatus"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}
        public int AddVersion(Model.Cms.cmsArticle ar, int oldid)
        {
            int id = dal.Add(ar);
            if (id != 0)
            {
                if (dal.AddVersion(id, oldid) > 0)
                {
                    return id;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public void UpHits(int id)
        {
            dal.UpHits(id);
        }

        public int ChangeOrder(int up, int down)
        {
            return dal.ChangeOrder(up, down);
        }
        #endregion  成员方法
    }
}

